package com.example.eschool;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
//
//import com.example.eschool.adapter.MypageAdapter;
import com.example.eschool.fragment.CardFragment;
import com.example.eschool.fragment.HomeFragment;
import com.example.eschool.fragment.MessageFragment;
import com.example.eschool.fragment.PersonFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_home,tv_card,tv_message,tv_person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        replaceFragment(new HomeFragment());
        tv_home.setOnClickListener(this);
        tv_card.setOnClickListener(this);
        tv_message.setOnClickListener(this);
        tv_person.setOnClickListener(this);
    }





    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_home:
                replaceFragment(new HomeFragment());
                break;
            case R.id.tv_card:
                replaceFragment(new CardFragment());
                break;
            case R.id.tv_message:
                replaceFragment(new MessageFragment());
                break;
            case R.id.tv_person:
                replaceFragment(new PersonFragment());
                break;
            default:break;
        }
    }


    //-------------------初始化数据-------------------
    private void initView() {
        tv_home= findViewById(R.id.tv_home);
        tv_card= findViewById(R.id.tv_card);
        tv_message=findViewById(R.id.tv_message);
        tv_person =findViewById(R.id.tv_person);
    }

        //-------------------Fragment-------------------
    public void replaceFragment(Fragment fragment){
        //fragment管理
        FragmentManager fragmentManager = getSupportFragmentManager();
        //创建
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //替换
        transaction.replace(R.id.main_fragment,fragment);
        //加入栈，使其可以返回上一个fragment
        transaction.addToBackStack(null);
        //结束关闭
        transaction.commit();
    }

}
